package org.example.everyday;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * Description:TODO 详细描述
 * <p>
 * <p>
 *
 * @author anlu
 * @date 2022/5/8 0008 21:11
 */
public class Lc442 {
    public static void main(String[] args) {
        Lc442 lc442 = new Lc442();
        int[] nums = {4,3,2,7,8,2,3,1};
        List<Integer> res= lc442.findDuplicates(nums);
        for (int i = 0; i < res.size(); i++) {
            System.out.println(res.get(i));
        }
    }

    public List<Integer> findDuplicates(int[] nums) {
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            while (nums[i] != nums[nums[i] - 1]){
                swap(nums,i,nums[i] - 1);
            }
        }
        List<Integer> ans = new ArrayList<Integer>();
        for (int i = 0; i < n; i++) {
            if(nums[i] - 1 != i){
                ans.add(nums[i]);
            }
        }
        return ans;
    }

    private void swap(int[] nums, int index1, int index2) {
        int temp = nums[index1];
        nums[index1] = nums[index2];
        nums[index2] = temp;
    }
}
